package service.impl;


import java.sql.Timestamp;

import common.impl.exception.ServiceException;
import model.MListen;
import net.sf.json.JSONObject;
import service.interf.IListen;
import dao.interf.IListenDao;
import dao.pojo.Exam;
import dao.pojo.Listen;


public class ListenImpl implements IListen{
	private IListenDao listenDao;

	
	public void setlistenDao(IListenDao listenDao) {
		this.listenDao = listenDao;
	}
	

	@Override
	public String add(MListen model) {
		Listen listen = new Listen();

		listen.setListenPassge(model.getListenPassge());
		listen.setListenDirection(model.getListenDirection());
		listen.setListenKeys(model.getListenKeys());

		return listenDao.add(listen);
	}
	
	@Override
	public String getById(MListen model) {
	Listen listen = listenDao.getById(model.getListenId());
	if(null == listen) {
		throw new ServiceException("ERR-0171");
	}
		return createListenObj(listen).toString();
	}

	@Override
	public String deleteById(Integer id) {
		listenDao.deleteById(id);
		return "1";
	}

	@Override
	public String edit(MListen model) {
		Listen listen = listenDao.getById(model.getListenId());
		if (null == listen) {
			throw new ServiceException("ERR-0171");
		}
				
			listen.setListenPassge(model.getListenPassge());
			listen.setListenDirection(model.getListenDirection());
			listen.setListenKeys(model.getListenKeys());
	
		listenDao.edit(listen);
		return "1";
	}
	
	public JSONObject createListenObj(Listen listen) {
		JSONObject obj = new JSONObject();
		obj.put("listenId", listen.getListenId());
		obj.put("listenDirection", listen.getListenDirection());
		obj.put("listenPassage", listen.getListenPassge());
		obj.put("listenKeys", listen.getListenKeys());		
		
		return obj;
	}


}
